<?php
class Model_Search extends Ac_Model
{
   const TYPE = 6;
   /*
    * This is a capped collection. must be created prior to use it
    * db.createCollection("search", {capped:true, size:100000})
    */
   protected static  $_collection = 'search';

    /**
     *
     * @var array define the requirements for this model
     */
    protected static $_requirements = array(
     //   'em'  => array('Required', 'Validator:EmailAddress'), //Email
    );

    protected static $_transcript = array(
        '_id'  => '_id',
        'us'   => 'user',
        'ur'   => 'url',
        'el'   => 'elements',
        'c_dt' => 'created_at',
    );

    public function __construct($data = array(), $config = array())
	{
        $config['collection'] = 'search';
        self::ensureIndex(array('c_dt' => 1));
        parent::__construct($data, $config);
    }
    
    public static function getListingInstance($category)
    {
        if (!empty($category)) {
            $classname = 'Model_Listing_'.ucfirst($category);
            $filename = APPLICATION_PATH .'/models/Listing/'.ucfirst($category).'.php';
            if ($res = Zend_Loader::isReadable($filename)) {
                    return $classname;
            }
            
            if(strlen($category) == 24) {
                $cat = Model_Category::find($category);
				// echo '<pre>';
				// print_r($cat);
				// echo '</pre>';
				// exit;
                if (!empty($cat) && !empty($cat->customization)) {
                    $classname = 'Model_Listing_'.ucfirst($cat->customization);
                    $filename = APPLICATION_PATH .'/models/Listing/'.ucfirst($cat->customization).'.php';
                    if ($res = Zend_Loader::isReadable($filename)) {
                            return $classname;
                    }
                }
            }
        }
        return 'Model_Listing_Regular';
    }
}